﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.ServiceModel.Configuration;
using System.Xml;

namespace Devtm.ServiceModel.Configurations
{

    /// <summary>
    /// Cette classe contient la meme logique que la section de configuration des bindings.
    /// le premier point important etant qu'elle a les même signatures 'BindingCollectionElement' que l'originale de Microsoft
    /// le deuxieme point est qu'elle dispose d'une methode de déserialisation 'ReadXml'
    /// 
    /// </summary>
    public sealed class CustomBindingsSection : ConfigurationSection
    {

        private static System.Configuration.Configuration configuration;
        //private ConfigurationPropertyCollection properties;


        /// <summary>													
        /// Updates the <see cref="LocatorSection" /> with the values from an <see cref="XmlReader" />.													
        /// </summary>													
        /// <param name="reader">The <see cref="XmlReader" /> that reads the configuration source located at the element that describes the configuration section.</param>													
        public void ReadXml(XmlReader reader)
        {
            if (reader == null) throw new ArgumentNullException("reader");


            DeserializeSection(reader);
        }


        /// <summary>													
        /// Writes the <see cref="LocatorSection" /> values as an XML element to an <see cref="XmlWriter" />.													
        /// </summary>													
        /// <param name="writer">The <see cref="XmlWriter" /> that writes to the configuration source.</param>													
        public void WriteXml(XmlWriter writer)
        {
            if (writer == null) throw new ArgumentNullException("writer");

            String serialized = SerializeSection(this, "LocatorSection", ConfigurationSaveMode.Full);
            writer.WriteRaw(serialized);
        }


        private Dictionary<string, BindingCollectionElement> BindingCollectionElements
        {
            get
            {
                Dictionary<string, BindingCollectionElement> dictionary = new Dictionary<string, BindingCollectionElement>();
                foreach (ConfigurationProperty property in this.Properties)
                {
                    dictionary.Add(property.Name, this[property.Name]);
                }
                return dictionary;
            }
        }

        public List<BindingCollectionElement> BindingCollections
        {
            get
            {
                List<BindingCollectionElement> list = new List<BindingCollectionElement>();
                foreach (ConfigurationProperty property in this.Properties)
                {
                    list.Add(this[property.Name]);
                }
                return list;
            }
        }

        private static System.Configuration.Configuration Configuration
        {
            get
            {
                return configuration;
            }
            set
            {
                configuration = value;
            }
        }


        #region Bindings


        [ConfigurationProperty("basicHttpBinding", Options = ConfigurationPropertyOptions.None)]
        public BasicHttpBindingCollectionElement BasicHttpBinding
        {
            get
            {
                return (BasicHttpBindingCollectionElement)base["basicHttpBinding"];
            }
        }

        [ConfigurationProperty("customBinding", Options = ConfigurationPropertyOptions.None)]
        public CustomBindingCollectionElement CustomBinding
        {
            get
            {
                return (CustomBindingCollectionElement)base["customBinding"];
            }
        }

        public new BindingCollectionElement this[string binding]
        {
            get
            {
                return (BindingCollectionElement)base[binding];
            }
        }

        [ConfigurationProperty("msmqIntegrationBinding", Options = ConfigurationPropertyOptions.None)]
        public MsmqIntegrationBindingCollectionElement MsmqIntegrationBinding
        {
            get
            {
                return (MsmqIntegrationBindingCollectionElement)base["msmqIntegrationBinding"];
            }
        }

        [ConfigurationProperty("netMsmqBinding", Options = ConfigurationPropertyOptions.None)]
        public NetMsmqBindingCollectionElement NetMsmqBinding
        {
            get
            {
                return (NetMsmqBindingCollectionElement)base["netMsmqBinding"];
            }
        }

        [ConfigurationProperty("netNamedPipeBinding", Options = ConfigurationPropertyOptions.None)]
        public NetNamedPipeBindingCollectionElement NetNamedPipeBinding
        {
            get
            {
                return (NetNamedPipeBindingCollectionElement)base["netNamedPipeBinding"];
            }
        }

        [ConfigurationProperty("netPeerTcpBinding", Options = ConfigurationPropertyOptions.None)]
        public NetPeerTcpBindingCollectionElement NetPeerTcpBinding
        {
            get
            {
                return (NetPeerTcpBindingCollectionElement)base["netPeerTcpBinding"];
            }
        }

        [ConfigurationProperty("netTcpBinding", Options = ConfigurationPropertyOptions.None)]
        public NetTcpBindingCollectionElement NetTcpBinding
        {
            get
            {
                return (NetTcpBindingCollectionElement)base["netTcpBinding"];
            }
        }

        [ConfigurationProperty("ws2007FederationHttpBinding", Options = ConfigurationPropertyOptions.None)]
        public WS2007FederationHttpBindingCollectionElement WS2007FederationHttpBinding
        {
            get
            {
                return (WS2007FederationHttpBindingCollectionElement)base["ws2007FederationHttpBinding"];
            }
        }

        [ConfigurationProperty("ws2007HttpBinding", Options = ConfigurationPropertyOptions.None)]
        public WS2007HttpBindingCollectionElement WS2007HttpBinding
        {
            get
            {
                return (WS2007HttpBindingCollectionElement)base["ws2007HttpBinding"];
            }
        }

        [ConfigurationProperty("wsDualHttpBinding", Options = ConfigurationPropertyOptions.None)]
        public WSDualHttpBindingCollectionElement WSDualHttpBinding
        {
            get
            {
                return (WSDualHttpBindingCollectionElement)base["wsDualHttpBinding"];
            }
        }

        [ConfigurationProperty("wsFederationHttpBinding", Options = ConfigurationPropertyOptions.None)]
        public WSFederationHttpBindingCollectionElement WSFederationHttpBinding
        {
            get
            {
                return (WSFederationHttpBindingCollectionElement)base["wsFederationHttpBinding"];
            }
        }

        [ConfigurationProperty("wsHttpBinding", Options = ConfigurationPropertyOptions.None)]
        public WSHttpBindingCollectionElement WSHttpBinding
        {
            get
            {
                return (WSHttpBindingCollectionElement)base["wsHttpBinding"];
            }
        }

        #endregion


    }

}
